package com.egao.orderB.biz.impl;

import com.egao.common.core.web.BaseDao;
import com.egao.common.core.web.PageResult2;
import com.egao.common.core.utils.Utils;
import com.egao.common.system.entity.User;
import com.egao.common.core.constants.UserConstant;
import com.egao.orderB.biz.MaintainFeeBiz;
import com.egao.orderB.entity.Address;
import com.egao.orderB.entity.MaintainFee;
import net.sf.json.JSONObject;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 *
 * 数据层实现类
 * @author : Doman
 * @date : 2020-06-15 14:20:45
 * @version ：1.0
 */
@Repository
public class MaintainFeeBizImpl extends BaseDao implements MaintainFeeBiz {

	@Override
	public JSONObject ntlist(PageResult2<T> page, MaintainFee maintainFee, User loginUser) throws Exception {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<MaintainFee> rm = new BeanPropertyRowMapper<MaintainFee>(MaintainFee.class);
		StringBuffer sql = new StringBuffer("select m.id,m.company_id,m.province");
        sql.append(",m.city,m.district,m.town,m.miles,m.dock");
		sql.append(",m.box_type,m.price,m.driver_price,m.commission");
		sql.append(",m.address,m.remark,m.create_time,m.create_user");
		sql.append(",m.update_time,m.update_user");
        sql.append(",cmy.name companyName,suser.username createName ");
        sql.append(" from chf_maintain_fee m ");
        sql.append(" inner join sys_user suser on suser.user_id=m.create_user ");
        sql.append(" inner join chf_company cmy on cmy.id=m.company_id ");
        sql.append(" where m.deleted=0 and cmy.deleted=0 ");
		if(loginUser.getUserId() != UserConstant.ADMIN_ID){
            sql.append(" and m.company_id=:tablecompanyId ");
            map.put("tablecompanyId", loginUser.getCompanyId() + "");
        }
    	if(Utils.isNotEmpty(maintainFee)){
			if(Utils.isNotEmpty(maintainFee.getProvince())){
//				sql.append(" and instr(m.province,:province)>0 ");
				sql.append(" and m.province=:province ");
				map.put("province", maintainFee.getProvince());
			}
			if(Utils.isNotEmpty(maintainFee.getCity())){
//				sql.append(" and instr(m.city,:city)>0 ");
				sql.append(" and m.city=:city ");
				map.put("city", maintainFee.getCity());
			}
			if(Utils.isNotEmpty(maintainFee.getDistrict())){
//				sql.append(" and instr(m.district,:district)>0 ");
				sql.append(" and m.district=:district ");
				map.put("district", maintainFee.getDistrict());
			}
			if(Utils.isNotEmpty(maintainFee.getTown())){
//				sql.append(" and instr(m.town,:town)>0 ");
				sql.append(" and m.town=:town ");
				map.put("town", maintainFee.getTown());
			}
			if(Utils.isNotEmpty(maintainFee.getBoxType())){
//				sql.append(" and instr(m.box_type,:box_type)>0 ");
				sql.append(" and m.box_type=:box_type ");
				map.put("box_type", maintainFee.getBoxType());
			}
			if(Utils.isNotEmpty(maintainFee.getAddress())){
//				sql.append(" and instr(m.address,:address)>0 ");
				sql.append(" and m.address=:address ");
				map.put("address", maintainFee.getAddress());
			}
			if(Utils.isNotEmpty(maintainFee.getDock())){
//				sql.append(" and instr(m.dock,:dock)>0 ");
				sql.append(" and m.dock=:dock ");
				map.put("dock", maintainFee.getDock());
			}
			if(Utils.isNotEmpty(maintainFee.getSort())&&Utils.isNotEmpty(maintainFee.getOrder())){
				sql.append(" order by "+maintainFee.getSort()+" "+maintainFee.getOrder());
			}
		}
		return jsonPage2(getPageQueryList(page, sql.toString(), map, rm));
	}

	@Override
	public List<MaintainFee> ntlist(MaintainFee maintainFee, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<MaintainFee> rm = new BeanPropertyRowMapper<MaintainFee>(MaintainFee.class);
		StringBuffer sql = new StringBuffer("select m.id,m.company_id,m.province");
		sql.append(",m.city,m.district,m.town,m.miles,m.dock");
		sql.append(",m.box_type,m.price,m.driver_price,m.commission");
		sql.append(",m.address,m.remark,m.create_time,m.create_user");
		sql.append(",m.update_time,m.update_user");
		sql.append(",cmy.name companyName,suser.username createName ");
		sql.append(" from chf_maintain_fee m ");
		sql.append(" inner join sys_user suser on suser.user_id=m.create_user ");
		sql.append(" inner join chf_company cmy on cmy.id=m.company_id ");
		sql.append(" where m.deleted=0 and cmy.deleted=0 ");
		if(loginUser.getUserId() != UserConstant.ADMIN_ID){
			sql.append(" and m.company_id=:tablecompanyId ");
			map.put("tablecompanyId", loginUser.getCompanyId() + "");
		}
		if(Utils.isNotEmpty(maintainFee)){
			if(Utils.isNotEmpty(maintainFee.getProvince())){
//				sql.append(" and instr(m.province,:province)>0 ");
				sql.append(" and m.province=:province ");
				map.put("province", maintainFee.getProvince());
			}
			if(Utils.isNotEmpty(maintainFee.getCity())){
//				sql.append(" and instr(m.city,:city)>0 ");
				sql.append(" and m.city=:city ");
				map.put("city", maintainFee.getCity());
			}
			if(Utils.isNotEmpty(maintainFee.getDistrict())){
//				sql.append(" and instr(m.district,:district)>0 ");
				sql.append(" and m.district=:district ");
				map.put("district", maintainFee.getDistrict());
			}
			if(Utils.isNotEmpty(maintainFee.getTown())){
//				sql.append(" and instr(m.town,:town)>0 ");
				sql.append(" and m.town=:town ");
				map.put("town", maintainFee.getTown());
			}
			if(Utils.isNotEmpty(maintainFee.getBoxType())){
//				sql.append(" and instr(m.box_type,:box_type)>0 ");
				sql.append(" and m.box_type=:box_type ");
				map.put("box_type", maintainFee.getBoxType());
			}
			if(Utils.isNotEmpty(maintainFee.getAddress())){
//				sql.append(" and instr(m.address,:address)>0 ");
				sql.append(" and m.address=:address ");
				map.put("address", maintainFee.getAddress());
			}
			if(Utils.isNotEmpty(maintainFee.getDock())){
//				sql.append(" and instr(m.dock,:dock)>0 ");
				sql.append(" and m.dock=:dock ");
				map.put("dock", maintainFee.getDock());
			}
		}
		getMapParameter(sql, map);
		return jdbcTemplate.query(sql.toString(),map, rm);
	}


	@Override
	public JSONObject list(PageResult2<T> page, MaintainFee maintainFee, User loginUser) throws Exception {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<MaintainFee> rm = new BeanPropertyRowMapper<MaintainFee>(MaintainFee.class);
		StringBuffer sql = new StringBuffer("select m.id,m.company_id,m.province");
        sql.append(",m.city,m.district,m.town,m.miles,m.dock");
		sql.append(",m.box_type,m.price,m.driver_price,m.commission");
		sql.append(",m.address,m.remark,m.create_time,m.create_user");
		sql.append(",m.update_time,m.update_user");
        sql.append(",cmy.name companyName,suser.username createName ");
        sql.append(" from chf_maintain_fee m ");
        sql.append(" inner join sys_user suser on suser.user_id=m.create_user ");
        sql.append(" inner join chf_company cmy on cmy.id=m.company_id ");
        sql.append(" where m.deleted=0 and cmy.deleted=0 ");
		if(loginUser.getUserId() != UserConstant.ADMIN_ID){
            sql.append(" and m.company_id=:tablecompanyId ");
            map.put("tablecompanyId", loginUser.getCompanyId() + "");
        }
    	if(Utils.isNotEmpty(maintainFee)){
			if(Utils.isNotEmpty(maintainFee.getProvince())){
				sql.append(" and instr(m.province,:province)>0 ");
				map.put("province", maintainFee.getProvince());
			}
			if(Utils.isNotEmpty(maintainFee.getCity())){
				sql.append(" and instr(m.city,:city)>0 ");
				map.put("city", maintainFee.getCity());
			}
			if(Utils.isNotEmpty(maintainFee.getDistrict())){
				sql.append(" and instr(m.district,:district)>0 ");
				map.put("district", maintainFee.getDistrict());
			}
			if(Utils.isNotEmpty(maintainFee.getTown())){
				sql.append(" and instr(m.town,:town)>0 ");
				map.put("town", maintainFee.getTown());
			}
			if(Utils.isNotEmpty(maintainFee.getBoxType())){
				sql.append(" and instr(m.box_type,:box_type)>0 ");
				map.put("box_type", maintainFee.getBoxType());
			}
			if(Utils.isNotEmpty(maintainFee.getAddress())){
				sql.append(" and instr(m.address,:address)>0 ");
				map.put("address", maintainFee.getAddress());
			}
			if(Utils.isNotEmpty(maintainFee.getDock())){
				sql.append(" and instr(m.dock,:dock)>0 ");
				map.put("dock", maintainFee.getDock());
			}
			if(Utils.isNotEmpty(maintainFee.getSort())&&Utils.isNotEmpty(maintainFee.getOrder())){
				sql.append(" order by "+maintainFee.getSort()+" "+maintainFee.getOrder());
			}
		}
		return jsonPage2(getPageQueryList(page, sql.toString(), map, rm));
	}

	@Override
	public List<MaintainFee> list(MaintainFee maintainFee, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<MaintainFee> rm = new BeanPropertyRowMapper<MaintainFee>(MaintainFee.class);
        StringBuffer sql = new StringBuffer("select m.id,m.company_id,m.province");
        sql.append(",m.city,m.district,m.town,m.miles,m.dock");
		sql.append(",m.box_type,m.price,m.driver_price,m.commission");
		sql.append(",m.address,m.remark,m.create_time,m.create_user");
		sql.append(",m.update_time,m.update_user");
        sql.append(",cmy.name companyName,suser.username createName ");
        sql.append(" from chf_maintain_fee m ");
        sql.append(" inner join sys_user suser on suser.user_id=m.create_user ");
        sql.append(" inner join chf_company cmy on cmy.id=m.company_id ");
        sql.append(" where m.deleted=0 and cmy.deleted=0 ");
		if(loginUser.getUserId() != UserConstant.ADMIN_ID){
            sql.append(" and m.company_id=:tablecompanyId ");
            map.put("tablecompanyId", loginUser.getCompanyId() + "");
        }
    	if(Utils.isNotEmpty(maintainFee)){
			if(Utils.isNotEmpty(maintainFee.getProvince())){
				sql.append(" and instr(m.province,:province)>0 ");
				map.put("province", maintainFee.getProvince());
			}
			if(Utils.isNotEmpty(maintainFee.getCity())){
				sql.append(" and instr(m.city,:city)>0 ");
				map.put("city", maintainFee.getCity());
			}
			if(Utils.isNotEmpty(maintainFee.getDistrict())){
				sql.append(" and instr(m.district,:district)>0 ");
				map.put("district", maintainFee.getDistrict());
			}
			if(Utils.isNotEmpty(maintainFee.getTown())){
				sql.append(" and instr(m.town,:town)>0 ");
				map.put("town", maintainFee.getTown());
			}
			if(Utils.isNotEmpty(maintainFee.getBoxType())){
				sql.append(" and instr(m.box_type,:box_type)>0 ");
				map.put("box_type", maintainFee.getBoxType());
			}
			if(Utils.isNotEmpty(maintainFee.getAddress())){
				sql.append(" and instr(m.address,:address)>0 ");
				map.put("address", maintainFee.getAddress());
			}
			if(Utils.isNotEmpty(maintainFee.getDock())){
				sql.append(" and instr(m.dock,:dock)>0 ");
				map.put("dock", maintainFee.getDock());
			}
		}
		getMapParameter(sql, map);
		return jdbcTemplate.query(sql.toString(),map, rm);
	}

	@Override
	public List<Address> searchAddressFee(MaintainFee maintainFee, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<Address> rm = new BeanPropertyRowMapper<Address>(Address.class);
        StringBuffer sql = new StringBuffer("select m.id,m.company_id,m.province");
        sql.append(",m.city,m.district,m.town,m.dock");
		sql.append(",m.box_type,m.price");
		sql.append(",m.address,m.remark,m.create_time,m.create_user");
		sql.append(",m.update_time,m.update_user");
        sql.append(",cmy.name companyName,suser.username createName ");
        sql.append(" from chf_maintain_fee m ");
        sql.append(" inner join sys_user suser on suser.user_id=m.create_user ");
        sql.append(" inner join chf_company cmy on cmy.id=m.company_id ");
        sql.append(" where m.deleted=0 and cmy.deleted=0 ");
		if(loginUser.getUserId() != UserConstant.ADMIN_ID){
            sql.append(" and m.company_id=:tablecompanyId ");
            map.put("tablecompanyId", loginUser.getCompanyId() + "");
        }
		sql.append(" and (instr(m.province,:province)>0 ");
		sql.append(" or instr(m.city,:province)>0 ");
		sql.append(" or instr(m.district,:province)>0 ");
		sql.append(" or instr(m.town,:province)>0 ");
		sql.append(" or instr(m.dock,:province)>0 ");
		sql.append(" or instr(m.box_type,:province)>0 ");
		sql.append(" or instr(m.commission,:province)>0 ");
		sql.append(" or instr(m.address,:province)>0 ");
		sql.append(" ) ");
		map.put("province", maintainFee.getProvince());
		getMapParameter(sql, map);
		return jdbcTemplate.query(sql.toString(),map, rm);
	}

	@Override
	public MaintainFee getById(MaintainFee maintainFee) {
		RowMapper<MaintainFee> rm = new BeanPropertyRowMapper<MaintainFee>(MaintainFee.class);
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql = new StringBuffer("select m.id,m.company_id,m.province");
        sql.append(",m.city,m.district,m.town,m.miles,m.dock");
		sql.append(",m.box_type,m.price,m.driver_price,m.commission");
		sql.append(",m.address,m.remark,m.create_time,m.create_user");
		sql.append(",m.update_time,m.update_user");
        sql.append(",cmy.name companyName,suser.username createName ");
        sql.append(" from chf_maintain_fee m ");
        sql.append(" inner join sys_user suser on suser.user_id=m.create_user ");
        sql.append(" inner join chf_company cmy on cmy.id=m.company_id ");
        sql.append(" where m.deleted=0 and cmy.deleted=0 and m.id=:id ");
		map.put("id", maintainFee.getId());
		getMapParameter(sql, map);
		return jdbcTemplate.queryForObject(sql.toString(), map, rm);
	}

	@Override
	public boolean save(MaintainFee maintainFee, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("insert into chf_maintain_fee (");
        sql.append("company_id,province,city,district");
		sql.append(",town,miles,dock,box_type,price");
		sql.append(",driver_price,commission,address,remark");
		sql.append(",create_user,create_time) values ( ");
        sql.append(":company_id,:province,:city,:district");
		sql.append(",:town,:miles,:dock,:box_type,:price");
		sql.append(",:driver_price,:commission,:address,:remark");
		sql.append(",:create_user,now())");
        map.put("company_id", loginUser.getCompanyId() + "");
        map.put("province", maintainFee.getProvince());
        map.put("city", maintainFee.getCity());
        map.put("district", maintainFee.getDistrict());
        map.put("town", maintainFee.getTown());
        map.put("miles", maintainFee.getMiles());
        map.put("dock", maintainFee.getDock());
        map.put("box_type", maintainFee.getBoxType());
        map.put("price", maintainFee.getPrice());
        map.put("driver_price", maintainFee.getDriverPrice());
        map.put("commission", maintainFee.getCommission());
        map.put("address", maintainFee.getAddress());
        map.put("remark", maintainFee.getRemark());
		map.put("create_user", loginUser.getUserId() + "");
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean update(MaintainFee maintainFee, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update chf_maintain_fee set ");
        sql.append("province=:province,city=:city,district=:district ");
		sql.append(",town=:town,miles=:miles,dock=:dock,box_type=:box_type ");
		sql.append(",price=:price,driver_price=:driver_price,commission=:commission ");
		sql.append(",address=:address,remark=:remark,update_user=:update_user where id=:id ");
        map.put("province", maintainFee.getProvince());
        map.put("city", maintainFee.getCity());
        map.put("district", maintainFee.getDistrict());
        map.put("town", maintainFee.getTown());
        map.put("miles", maintainFee.getMiles());
        map.put("dock", maintainFee.getDock());
        map.put("box_type", maintainFee.getBoxType());
        map.put("price", maintainFee.getPrice());
        map.put("driver_price", maintainFee.getDriverPrice());
        map.put("commission", maintainFee.getCommission());
        map.put("address", maintainFee.getAddress());
        map.put("remark", maintainFee.getRemark());
		map.put("update_user", loginUser.getUserId() + "");
		map.put("id", maintainFee.getId());
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean remove(MaintainFee maintainFee, User loginUser) {
		int count = 0;
		Map<String, String> map = null;
		if(maintainFee.getId().length()>0){
			for(String ID : maintainFee.getId().split(",")){
				map  = new HashMap<String, String>();
				if(ID==null || ID.equals("")){ break; }
				StringBuffer sql =new StringBuffer("update chf_maintain_fee set deleted=1" +
						",update_user=:update_user, update_time=now() "
						+ " where id =:id ");
				map.put("update_user", loginUser.getUserId() + "");
				map.put("id", ID);
				getMapParameter(sql, map);
				count = jdbcTemplate.update(sql.toString(),map);
			}
		}
		return count > 0;
	}
}
